(function($){
	/*树的简单封装*/
	var _options, ztreeObj;
	var JiePosTree = function(options){
		_options = $.extend({}, JiePosTree.DEFAULT_OPTION, options);
		showSearch(_options);
		/*加载树数据*/
		ztreeObj = $.fn.zTree.init($("#" + _options.treeEl), initSetting());
		/*打开模态框*/
		$("#" + _options.winEl).jpwin(_options, _options.confirmCallback != null ? confirmCallback : null);
		return ztreeObj;
	};
	
	/*树的默认配置*/
	JiePosTree.DEFAULT_OPTION = {
		name: "NAME", // 文本属性取值key
		idKey: "ID", // 节点属性取值key
		pIdKey: "PID", // 父节点取值key
		treeEl: "jpTree", // 树标识
		winEl: "jpWin", // 窗口标识
		title: "", // 窗口标题
		chkboxType: {"Y": "ps", "N": "ps"},
		url: null, // 加载数据的地址
		showCheckBox: false, // 是否显示复选框
		showSearch: false, // 是否显示搜索框
		searchTitle: null, // 搜索框标题
		confirmCallback: null, // 确认按钮回调函数
		openIds: null, // 展开的节点key的集合(字符串用逗号隔开)
		resizable:true
	};
	
	/*参数初始化*/
	initSetting = function(){
		var setting = {
			/*显示复选框*/
			check: {
				enable: _options.showCheckBox,
				chkboxType: _options.chkboxType
			},
			/*简单json格式*/
			data: {
				key: {name: _options.name},
				simpleData:{enable: true, idKey: _options.idKey, pIdKey: _options.pIdKey}
			},
			/*异步加载数据配置*/
			async: {
				enable: true,
				url: _options.url,
				autoParam: [_options.idKey]
			},
			view: {
				selectedMulti: true,
				expandSpeed: 100 // 加载速度100毫秒
			},
			callback: {
//				beforeClick: beforeClick,
				onAsyncSuccess: onAsyncSuccess
			}
		};
		return setting;
	};
	
	/*点击事件*/
	beforeClick = function(treeId, treeNode){
		alter($(".tree-search").val());
		if(!treeNode.isParent){
			return false;
		}
		return true;
	}
	
	/*异步加载成功回调函数*/
	onAsyncSuccess = function(event, treeId, treeNode, msg){
		/*搜索结果全部展开*/
		if(_options.search){
			ztreeObj.expandAll(true);
			return;
		}
		/*默认展示第一级*/
		if(!_options.openIds){
			ztreeObj.expandNode(ztreeObj.getNodesByFilter(function (node) { return node.level == 0 }, true), true, false, true);
			return;
		}
		/*类型转换*/
		if(typeof _options.openIds === "string"){
			_options.openIds = _options.openIds.split(",");
		}
		/*展开选中的节点*/
		for(var val in _options.openIds){
			var treeNode = ztreeObj.getNodeByParam(_options.idKey, _options.openIds[val], null);
			/*展开节点*/
			ztreeObj.expandNode(treeNode, true, true, true);
			if(_options.showCheckBox){
				ztreeObj.checkNode(treeNode, true, true);// 节点复选框置为选中状态
			}
			ztreeObj.selectNode(treeNode);
		}
	}
	
	/*点击确认按钮，默认回调函数*/
	confirmCallback = function(){
		if(ztreeObj && _options.showCheckBox){
			_options.confirmCallback(ztreeObj.getCheckedNodes());// 返回选择的树节点
		}else if(ztreeObj && !_options.showCheckBox){
			_options.confirmCallback(ztreeObj.getSelectedNodes());// 返回选择的树节点
		}
	}
	
	/*显示搜索框和树*/
	showSearch = function(_options){
		var win = "#"+_options.winEl;
		if(_options.showSearch){
			$(win).html("<div class='ins-search'><span>"
					+_options.searchTitle
					+"：</span><input class='easyui-textbox tree-search' type='text'/>"
					+"<a href='javascript:void(0)' class='easyui-linkbutton btn' iconCls='icon-search' onclick='treeSearch()'>搜索</a></div>");
			$(win).append('<div id="'+_options.treeEl+'" class="ztree"></div>');
			_options.cache = false;
		}else{
			$(win).html('<div id="'+_options.treeEl+'" class="ztree"></div>');
		}
	}
	
	/*搜索按钮事件*/
	treeSearch = function(){
		var searchText = $(".tree-search").val();
		if(!searchText){
			return;
		}
		ztreeObj.setting.async.otherParam = {searchText: searchText};
		ztreeObj.reAsyncChildNodes(null, "refresh");
		_options.search = true;
	}
	
	$.fn.jpTree = function(options){
		return new JiePosTree(options);
	};
})($)